//
//  TNTranslateTableViewCell.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/22.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNTranslateTableViewCell.h"
#import "Macro.h"

@interface TNTranslateTableViewCell ()

@property(nonatomic, strong) UIImageView *pic;

@end

@implementation TNTranslateTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGFloat width = SCREEN_WIDTH - 20;
        _pic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, width, 80)];
        [_pic setImage:[UIImage imageNamed:@"TCButtonBackgroundHighlight"]];
        _pic.userInteractionEnabled = YES;
        [self.contentView addSubview:_pic];
        _lable0 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, width - 40, 0)];
        _lable0.numberOfLines = 0;
        [_pic addSubview:_lable0];
        _lable1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, width - 40, 0)];
        _lable1.numberOfLines = 0;
        [_pic addSubview:_lable1];
        _button0 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button0 setImage:[UIImage imageNamed:@"FullScreen_Normal"] forState:UIControlStateNormal];
        _button0.frame = CGRectMake(80 , 60, 20, 20);
        [_pic addSubview:_button0];
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button1 setImage:[UIImage imageNamed:@"Delete_Normal"] forState:UIControlStateNormal];
        _button1.frame = CGRectMake(width - 100 , 60, 20, 20);
        [_pic addSubview:_button1];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setModel:(TNTranslateModel *)model
{
    _model = model;
    _lable0.text = model.dst;
    CGRect rect = _lable0.frame;
    rect.size.height = [TNTranslateTableViewCell heightOfString:model.dst];
    _lable0.frame = rect;
    _lable1.text = model.src;
    rect = _lable1.frame;
    rect.origin.y = CGRectGetMaxY(_lable0.frame) + 10;
    rect.size.height = [TNTranslateTableViewCell heightOfString:model.src];
    _lable1.frame = rect;
    rect = _button0.frame;
    rect.origin.y = CGRectGetMaxY(_lable1.frame) + 10;
    _button0.frame = rect;
    rect.origin.x = _button1.frame.origin.x;
    _button1.frame = rect;
    rect = _pic.frame;
    rect.size.height = CGRectGetMaxY(_button0.frame) + 10;
    _pic.frame = rect;
}

+ (CGFloat)heightOfString:(NSString *)str
{
    return [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil].size.height;
}

+ (CGFloat)heightForCell:(TNTranslateModel *)model
{
    return 10 + [self heightOfString:model.dst] + 10 + [self heightOfString:model.src] + 10 + 20 + 10 + 10;
}

@end
